package com.wang.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wang.common.dao.impl.BaseDaoImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.dao.IAEmailDao;
import com.wang.pojo.AEmail;

/**
 * 管理员dao实现类
 * @author Zjx
 *
 */
public class AEmailDaoImpl extends BaseDaoImpl<AEmail> implements IAEmailDao
{

	@Override
	public int saveOne(AEmail t)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_email (tag,rcpt,sender,region,ip,fromname,url,authuser,content,status,createTime,updateTime,pubTime,sendTime) ");
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
		
		paramsList.add(t.getTag());
		paramsList.add(t.getRcpt());
		paramsList.add(t.getSender());
		paramsList.add(t.getRegion());
		paramsList.add(t.getIp());
		paramsList.add(t.getFromname());
		paramsList.add(t.getUrl());
		paramsList.add(t.getAuthuser());
		paramsList.add(t.getContent());
		
		paramsList.add(t.getStatus());
		paramsList.add(t.getCreateTime());
		paramsList.add(t.getUpdateTime());
		paramsList.add(t.getPubTime());
		paramsList.add(t.getSendTime());
		
		/**
		 * 执行sql语句
		 */
		int res =  executeSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		t.setId(id);
		return res ; 
	}
	
	@Override
	public int[] saveBatch(List<AEmail> list)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_email (tag,rcpt,sender,region,ip,fromname,url,authuser,content,status,createTime,updateTime,pubTime,sendTime) ");
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
		
		List<List<Object>> paramsList = new ArrayList<List<Object>>();
		/* 参数列表 */
		for (AEmail t : list) {
			List<Object> paramsTempList = new ArrayList<Object>();
			paramsTempList.add(t.getTag());
			paramsTempList.add(t.getRcpt());
			paramsTempList.add(t.getSender());
			paramsTempList.add(t.getRegion());
			paramsTempList.add(t.getIp());
			paramsTempList.add(t.getFromname());
			paramsTempList.add(t.getUrl());
			paramsTempList.add(t.getAuthuser());
			paramsTempList.add(t.getContent());
			
			paramsTempList.add(t.getStatus());
			paramsTempList.add(t.getCreateTime());
			paramsTempList.add(t.getUpdateTime());
			paramsTempList.add(t.getPubTime());
			paramsTempList.add(t.getSendTime());
			
			paramsList.add(paramsTempList);
		}
		
		/**
		 * 执行sql语句
		 */
		int[] res =  executeBatchSql(sb.toString(),paramsList);
		return res ; 
	}

	@Override
	public int updateOne(AEmail t)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_email set ");
		sb.append("tag=?,rcpt=?,sender=?,region=?,ip=?,fromname=?,url=?,authuser=?,content=?,status=?,createTime=?,updateTime=?,pubTime=?,sendTime=?");
		sb.append(" where id = ? ");
		
		paramsList.add(t.getTag());
		paramsList.add(t.getRcpt());
		paramsList.add(t.getSender());
		paramsList.add(t.getRegion());
		paramsList.add(t.getIp());
		paramsList.add(t.getFromname());
		paramsList.add(t.getUrl());
		paramsList.add(t.getAuthuser());
		paramsList.add(t.getContent());
		paramsList.add(t.getStatus());
		paramsList.add(t.getCreateTime());
		paramsList.add(t.getUpdateTime());
		paramsList.add(t.getPubTime());
		paramsList.add(t.getSendTime());
		paramsList.add(t.getId());
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_email ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public AEmail findOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_email ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else if(condMap.get("name") != null && !"".equalsIgnoreCase(condMap.get("tag") + ""))
		{
			/* 邮件编号查询 */
			sb.append(" where tag = ? ");
			paramsList.add(condMap.get("tag"));
		}else
		{
			return null ; 
		}
		
		return this.findOneSql(sb.toString(), paramsList,AEmail.class);
	}

	@Override
	public List<AEmail> findCondList(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_email where 1 = 1 ");
		
		/* 关键字 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sb.append(" and (tag like ? or fromname like ? ) ");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sb.append(" and status = ?  ");
			paramsList.add(condMap.get("status"));
		}
		
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and lastLoginTime >= ? and lastLoginTime < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		/* 排序 */
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id desc ") ; 
		}else
		{
			sb.append(" order by id desc ");
		}
		
		if(pageInfoUtil != null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,AEmail.class);
		}
		return this.findListSql(sb.toString(), paramsList,AEmail.class); 
	}

	

}
